package cn.wolfcode.service.impl;

import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.Product;
import cn.wolfcode.domain.SeckillProduct;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.feign.ProductFeignApi;
import cn.wolfcode.mapper.OrderInfoMapper;
import cn.wolfcode.mapper.SeckillProductMapper;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.ISeckillProductService;

import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
@Slf4j
public class SeckillProductServiceImpl implements ISeckillProductService {
    @Autowired
    private SeckillProductMapper seckillProductMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private ProductFeignApi productFeignApi;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Override
    public List<SeckillProductVo> querySeckillProduct(Integer time) {
        //1.查询秒杀商品
        List<SeckillProduct> seckillProducts = seckillProductMapper.queryCurrentlySeckillProduct(time);
        //2.把ids放到list中
        List<Long> ids=new ArrayList<>();
        for (SeckillProduct seckillProduct : seckillProducts) {
            ids.add(seckillProduct.getProductId());
        }
        //3.远程调用商品服务,查询商品
        Result<List<Product>> result = productFeignApi.queryProductByIds(ids);
        List<Product> products= result.getData();
        if(result!=null&&!result.hasError()){
            //4.进行聚合操作
            Map<Long,Product> data=new HashMap<>();
            for (Product product : products) {
                data.put(product.getId(),product);
            }
            List<SeckillProductVo> vos=new ArrayList<>();
            for (SeckillProduct seckillProduct : seckillProducts) {
                Product product = data.get(seckillProduct.getProductId());
                SeckillProductVo vo=new SeckillProductVo();
                BeanUtils.copyProperties(product,vo);
                BeanUtils.copyProperties(seckillProduct,vo);
                vos.add(vo);
            }
            return vos;
         }

        return null;
    }

    @Override
    public List<SeckillProductVo> queryByTime(Integer time) {
        List<SeckillProductVo> vo=new ArrayList<>();
        List<Object> list = redisTemplate.opsForHash().values(SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time + ""));
        for (Object object : list) {
            SeckillProductVo seckillProductVo = JSON.parseObject(object + "", SeckillProductVo.class);
            vo.add(seckillProductVo);
        }
        return vo;
    }

    @Override
    public SeckillProductVo find(Integer time, Long seckillId) {
        String key=SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time+"");
        Object ps = redisTemplate.opsForHash().get(key, seckillId + "");
        SeckillProductVo vo = JSON.parseObject(ps + "", SeckillProductVo.class);
        return vo;
    }

    @Override
    public void syncRedisStockCount(Long seckillId,Integer time) {
        int stockCount = seckillProductMapper.getStockCount(seckillId);
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_STOCK_COUNT_HASH.getRealKey(time+""),seckillId+"",stockCount+"");
            log.info("数据回滚");
    }
}
